package com.example.dazuoye.controller;

import com.example.dazuoye.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.example.dazuoye.controller.Filenamegen;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Controller
public class FileController {
    @Autowired
    private UserService service;
    private String host;
    private String rootPath = "/etc";
    private String sonPath = "/456/";
    //图片的url路径
    private String imgPath;
    private static final Logger logger =  LoggerFactory.getLogger(FileController.class);
    @javax.annotation.Resource
    private ResourceLoader resourceLoader;
    @RequestMapping(value = "/upload")
    @ResponseBody
    public String upload(@RequestParam("test")MultipartFile file,@RequestParam("u_id")String u_id)
    {
        if(file.isEmpty())
            return "文件为空";
            host = "https://www.hkzhaoscut.top";
        //String fileName = file.getOriginalFilename();
        Filenamegen filenamegen = new Filenamegen();
        //得到后缀 拼接 哈哈哈
        String originfilename = file.getOriginalFilename();
        String suffix = originfilename.substring(originfilename.lastIndexOf(".")+1);
        //拼接 过于真实
        String fileName =  filenamegen.getfname()+"."+suffix;
        String filePath = rootPath+sonPath;
        logger.info("上传的文件路径"+filePath);
        logger.info("整个图片路径："+host+":" +sonPath+fileName);
        File dest = new File(filePath+fileName);
        String imgPath = (host+":" + sonPath +fileName).toString();
        //如果用到Linux中 需要改变图片的中文路径
        //fileName = UUID.randomUUID()+suffixName;
        if(!dest.getParentFile().exists()){
            dest.getParentFile().mkdirs();
        }
        try{
            file.transferTo(dest);
            logger.info("有了有了");

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        service.insertlicensepic(u_id,imgPath);
        logger.info("有了有了");
        return "wofole";
    }

   /* @RequestMapping("/test")
    public String test(){
        return "index.html";
    }

    @RequestMapping("/456/{filename:.+}")
    @ResponseBody
    public ResponseEntity<Resource> show (@PathVariable String filename)
    {
        try
        {
            //resourceLoader.getResource("file:" + uploadPicturePath + fileName) 返回指定路径的资源句柄，这里返回的就是URL [file:D:/springboot/upload/test.png]
            //ResponseEntity.ok(T) 返回指定内容主体
            return ResponseEntity.ok(resourceLoader.getResource("file:" + rootPath+sonPath + filename));
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }*/
}
